import { Injectable, Logger } from '@nestjs/common'
import { PassportStrategy } from '@nestjs/passport'
import { ExtractJwt, Strategy } from 'passport-jwt'

interface AuthResult {
  mobile: string
  password: string
  iat: number
  exp: number
}

@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
  constructor() {
    super({
      jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
      ignoreExpiration: false,
      jsonWebTokenOptions: {
        algorithms: 'HS256'
      },
      secretOrKey: 'json_web_token_secret_key'
    })
  }

  public async validate(payload: AuthResult) {
    Logger.log('认证通过', payload)
    //payload：jwt-passport认证jwt通过后解码的结果
    return payload
  }
}
